<?php

/**
 * @author Jeevan Tomson
 * @copyright 2011
 */

class FormValidator {
    
    public function validateLoginInputData($data) {
        $error_msgs = array();
        
        global $logintypes;
        
        $logintype = ( isset($data["logintype"]) && $data["logintype"] != "" && in_array($data["logintype"], $logintypes)  ) ? $data["logintype"] : "";

        if( $logintype == "" ) {
            array_push($error_msgs,"Invalid Login Type.");
        } else {
            $this->addErrorMsgs($error_msgs,$this->validateUserName($data["username"]));
            $this->addErrorMsgs($error_msgs,$this->validatePassword($data["password"]));
            
            if($logintype == "s") {
                if( !isset($data["shop"]) || trim($data["shop"]) == "" ) {
                   array_push($error_msgs,"Select a shop to login."); 
                }
            } else if($logintype == "b") {
                if( !isset($data["bakery"]) || trim($data["bakery"]) == "" ) {
                   array_push($error_msgs,"Select a bakery to login."); 
                }
            }
        }
        return $error_msgs;
    }
    
    public function validateUpdateUserData($userid, $name, $actype, $disabled, $mobile, $email, $address, $comment) {
        $error_msgs = array();
        
        $this->addErrorMsgs($error_msgs,$this->validateRowId($userid, "User Id"));
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateAccountType($actype));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($mobile, "Mobile"));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateUpdateShopData($shopid, $name, $phone, $fax, $email, $address, $colorcode, $comment) {
        $error_msgs = array();
        
        $this->addErrorMsgs($error_msgs,$this->validateRowId($shopid, "Shop Id"));
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($fax, "Fax", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateColorCode($colorcode));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateUpdateBakeryData($bakeryid, $name, $address, $phone, $fax, $email, $comment) {
        $error_msgs = array();

        $this->addErrorMsgs($error_msgs,$this->validateRowId($bakeryid, "Bakery Id"));
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($fax, "Fax", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateUpdateDriverData($driverid, $name, $mobile, $phone, $email, $comment) {
        $error_msgs = array();

        $this->addErrorMsgs($error_msgs,$this->validateRowId($driverid, "Driver Id"));
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($mobile, "Mobile"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateAddUserData( $name, $usertype, $username, $password, $mobile, $email, $address, $comment = "" ) {
        $error_msgs = array();

        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateAccountType($usertype));
        $this->addErrorMsgs($error_msgs,$this->validateUserName($username));
        $this->addErrorMsgs($error_msgs,$this->validatePassword($password));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($mobile, "Mobile"));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;    
    }
    
    public function validateAddShopData( $name, $phone, $fax = "", $email = "", $address, $colorcode, $comment = "" ) {
        $error_msgs = array();
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($fax, "Fax", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateColorCode($colorcode));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        return $error_msgs;
    }
    
    public function validateAddBakeryData( $name, $address, $phone, $fax, $email, $comment ) {
        $error_msgs = array();
        
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($fax, "Fax", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateAddOrderData( $cname, $deladdress, $deldate, $deltime ) {
        $error_msgs = array();
        
        $this->addErrorMsgs($error_msgs,$this->validateName($cname));

        return $error_msgs;
    }
    
    public function validateAddDriverData( $name, $mobile, $phone, $email, $comment ) {
        $error_msgs = array();
        
        $this->addErrorMsgs($error_msgs,$this->validateName($name));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($mobile, "Mobile"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($phone, "Phone", false));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email, false));
        $this->addErrorMsgs($error_msgs,$this->validateComment($comment));
        
        return $error_msgs;
    }
    
    public function validateChangePasswordData($userid, $oldpswd, $newpswd, $confirmpswd) {
        $error_msgs = array();
        $this->addErrorMsgs($error_msgs,$this->validatePassword($oldpswd, "Old password"));
        $this->addErrorMsgs($error_msgs,$this->validatePassword($newpswd, "New password"));
        $this->addErrorMsgs($error_msgs,$this->validatePassword($confirmpswd, "Confirm password"));
        $this->addErrorMsgs($error_msgs,$this->validateConfirmPassword( $newpswd, $confirmpswd ));
        return $error_msgs;
    }
    
    public function validateAccUpdateData($userid, $mobile, $email, $address) {
        $error_msgs = array();
        $this->addErrorMsgs($error_msgs,$this->validateRowId($userid, "User Id"));
        $this->addErrorMsgs($error_msgs,$this->validateMobile($mobile, "Mobile"));
        $this->addErrorMsgs($error_msgs,$this->validateEmail($email));
        $this->addErrorMsgs($error_msgs,$this->validateAddress($address));
        return $error_msgs;
    }
    
    private function addErrorMsgs( &$errors, $new ){
        foreach ( $new as $error ){
            if($error != "") {
                array_push($errors, $error);
            }
        }
    }
    
    private function validateRowId($id, $fieldname = "Id") {
        $error_msgs = array();

        if( !isset($id) || trim($id) == "" ) {
            array_push($error_msgs,$fieldname." cannot be empty.");
        } else {
            $int_options = array("options"=>array("min_range"=>0));
            if( filter_var($var, FILTER_VALIDATE_INT, $int_options) ) {
                array_push($error_msgs,$fieldname." should be an number.");
            }
        }
        return $error_msgs;
    }
    
    private function validateAddress($address, $required = true){
        $error_msgs = array();
        if( !isset($address) || trim($address) == "" ) {
            if($required)
                array_push($error_msgs,"Address cannot be empty.");
        } else { 
            if( strlen($address) > ADDRESS_MAX_LENGTH ) {
                array_push($error_msgs,"Address should be maximum ".ADDRESS_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;       
    }
    
    private function validateComment($comment){
        $error_msgs = array();
        if( !isset($comment) || trim($comment) == "" ) {
            
        } else { 
            if( strlen($comment) > COMMENT_MAX_LENGTH ) {
                array_push($comment,"Comment should be maximum ".COMMENT_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;       
    }
    
    private function validateMobile($phone, $fieldname, $required = true){
        $error_msgs = array();
        if(!isset($fieldname))
            $fieldname = "Mobile";
        if( !isset($phone) || trim($phone) == "" ) {
            if($required)
                array_push($error_msgs,$fieldname." cannot be empty.");
        } else {
            
            if( strlen($phone) < PHONE_MIN_LENGTH ) {
                array_push($error_msgs,$fieldname." should be minimum ".PHONE_MIN_LENGTH." characters.");
            } 
            
            if( strlen($phone) > PHONE_MAX_LENGTH ) {
                array_push($error_msgs,$fieldname." should be maximum ".USER_NAME_MAX_LENGTH." characters.");
            }
        }
        
        return $error_msgs;       
    }
    
    private function validateEmail($email, $required = true) {
        $error_msgs = array();
        if( !isset($email) || trim($email) == "" ) {
            if($required)
                array_push($error_msgs,"Email cannot be empty.");
        } else {
            if( strlen($email) < EMAIL_MIN_LENGTH ) {
                array_push($error_msgs,"Email should be minimum ".EMAIL_MIN_LENGTH." characters.");
            }
            if( strlen($email) > EMAIL_MAX_LENGTH ) {
                array_push($error_msgs,"Email should be maximum ".EMAIL_MAX_LENGTH." characters.");
            }
            if( !$this->isValidEmail($email) ) {
                array_push($error_msgs,"Invalid email entered.");
            }
        }
        return $error_msgs;
    }
    
    private function validateColorCode($colorcode, $required = true) {
        $error_msgs = array();
        if( !isset($colorcode) || trim($colorcode) == "" ) {
            if($required)
                array_push($error_msgs,"Color code cannot be empty.");
        } else {
            if( strlen($colorcode) < COLORCODE_MIN_LENGTH ) {
                array_push($error_msgs,"Color code should be minimum ".COLORCODE_MIN_LENGTH." characters.");
            }
            if( strlen($colorcode) > COLORCODE_MAX_LENGTH ) {
                array_push($error_msgs,"Color code should be maximum ".COLORCODE_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;
    }
    
    
    
    private function isValidEmail($email) {
        if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return true;
        } else {
            return false;
        }
    }
    
    private function validateAccountType($acType, $fieldname = "Account type") {
        $error_msgs = array();
        if( !isset($acType) || trim($acType) == "" ) {
            array_push($error_msgs, $fieldname." cannot be empty.");
        } else {
            global $logintypes;
            if( in_array($acType, $logintypes) ? false : true ) {
                array_push($error_msgs, "Invalid ".$fieldname);
            }
        }
        return $error_msgs;        
    } 
    
    private function validateUserName($username){
        $error_msgs = array();
        if( !isset($username) || trim($username) == "" ) {
            array_push($error_msgs,"Username cannot be empty.");
        } else {
            if( strlen($username) < USER_NAME_MIN_LENGTH ) {
                array_push($error_msgs,"Username should be minimum ".USER_NAME_MIN_LENGTH." characters.");
            } 
            if( strlen($username) > USER_NAME_MAX_LENGTH ) {
                array_push($error_msgs,"Username should be maximum ".USER_NAME_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;       
    }
    
    private function validateName($username, $fieldname = "Name"){
        $error_msgs = array();
        if( !isset($username) || trim($username) == "" ) {
            array_push($error_msgs, $fieldname." cannot be empty.");
        } else {
            if( strlen($username) < NAME_MIN_LENGTH ) {
                array_push($error_msgs, $fieldname." should be minimum ".NAME_MIN_LENGTH." characters.");
            }
            if( strlen($username) > NAME_MAX_LENGTH ) {
            array_push($error_msgs, $fieldname." should be maximum ".NAME_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;       
    }
    
    private function validatePassword($password, $fieldname = "Password"){
        $error_msgs = array();
        if(!isset($fieldname))
            $fieldname = "Password";
        if( !isset($password) || trim($password) == "" ) {
            array_push($error_msgs, $fieldname." cannot be empty.");
        } else {
            if( strlen($password) < PASSWORD_MIN_LENGTH ) {
                array_push($error_msgs,$fieldname." should be minimum ".PASSWORD_MIN_LENGTH." characters.");
            } 
            if( strlen($password) > PASSWORD_MAX_LENGTH ) {
                array_push($password,$fieldname." should be maximum ".PASSWORD_MAX_LENGTH." characters.");
            }
        }
        return $error_msgs;       
    }
    
    
    private function validateConfirmPassword( $pswd, $confpswd ) {
        $error_msgs = array();
        if( $pswd != $confpswd ) {
            array_push($error_msgs, "Confirm password should be same as password.");
        }
        return $error_msgs; 
    }
    
    
}

?>